package com.jc.service.api.order;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.DateTimeUtil;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.OrderProducts;
import com.jc.ds.model.Supplier;
import com.jc.ds.model.SupplierProduct;
import com.jc.ds.model.Xorder;
import com.jc.ds.model.XorderProcess;
import com.jc.framework.enums.Bind;
import com.jc.framework.enums.Booleans;
import com.jc.framework.enums.IsChildGoods;
import com.jc.framework.enums.OrderStatus;
import com.jc.framework.enums.PaidStatus;
import com.jc.framework.enums.SystemUser;
import com.jc.framework.util.SpringUtils;
import com.jc.framework.web.ResponseResultModel;
import com.jc.service.api.BaseDual;
import com.jc.service.order.XorderService;
import com.jc.service.supplier.SupplierService;
import com.jc.service.supplierproduct.SupplierProductService;
import com.jc.service.xorderprocess.XorderProcessService;


public class add extends BaseDual<ChannelOrder> {
	
	SupplierProductService supplierProductService = SpringUtils.getBean(SupplierProductService.class);
	XorderService orderService = SpringUtils.getBean(XorderService.class);
	SupplierService supplierService = SpringUtils.getBean(SupplierService.class);
	XorderProcessService xorderProcessService = SpringUtils.getBean(XorderProcessService.class);
	public ResponseEntity<?> dual(JSONObject data ) {	
		ChannelOrder order = getData(data);
		if(order == null) {		
			throw new BaseServiceException("缺少业务实体参数");
		}
		if(StringUtils.isBlank(order.getChannelOrderId())) {
			throw new BaseServiceException("缺少必填参数: channelOrderId");
		}
		Xorder o = packageOrderData(order);
		List<OrderProducts> plist = packageOrderProductsData(order);
		orderService.addOrder(o, plist);
		if(o.getId() != null && StringUtils.isNotBlank(o.getChannelOrderId())) {
			xorderProcessService.addXorderProcess(o.getChannelOrderId(), "由APP商城推送到 OMS系统", SystemUser.ADMIN.getName(), o.getId());
		}
		return new ResponseEntity<>(new ResponseResultModel<>(null),HttpStatus.OK);
	}
	
	
	public List<OrderProducts> packageOrderProductsData(ChannelOrder channelOrder){
		List<OrderProducts> productList = new ArrayList<OrderProducts>();
		List<OrderProducts> _list = channelOrder.getProducts();
		for(OrderProducts sku : _list) {
			 String skuid = sku.getSupplierSkuId();
			 Long id = 0L;
			 try {
				id = Long.valueOf(skuid);
			 }catch (Exception e) {
				throw new BaseServiceException("SKU节点中, supplierSkuId 这个参数不能超出LONG型范围");
			 }
			 SupplierProduct product = supplierProductService.queryById(id);
			 if(product == null) {
				 throw new BaseServiceException("SKU节点中, supplierSkuId 这个节点不是通过OMS系统推送过去的skuCode ");
			 }
			 
			 if(product != null && StringUtils.isNotBlank(product.getProductId()) ) {
				 sku.setSupplierProductId(product.getProductId());
			 }
			 if(sku.getSubGoods() == null) {
				 sku.setSubGoods(IsChildGoods.CHILD_GOODS.getName());//默认是子商品
			 }
			 if(StringUtils.isBlank(sku.getIsGift())) {
				 sku.setIsGift(Booleans.FALSE.getName());
			 }
			 if(sku.getIsBind() == null) {
				 sku.setIsBind(Bind.NOT.getName());
			 }
			 sku.setIsDelete(Booleans.FALSE.getName());
			 sku.setId(null);
			 productList.add(sku);
		}
		return productList;
	}
	
	public Xorder packageOrderData(ChannelOrder channelOrder) {
		Xorder o = new Xorder();
		o.setChannelOrderId(channelOrder.getChannelOrderId());
		if(StringUtils.isNotBlank(channelOrder.getpId())) o.setpId(channelOrder.getpId());
		if(StringUtils.isNotBlank(channelOrder.getOrderType()))o.setOrderType(channelOrder.getOrderType());
		if(StringUtils.isBlank(channelOrder.getChannelSource())) {
			throw new BaseServiceException("缺少必填参数 channelSource");
		}
		o.setChannelSource(channelOrder.getChannelSource());
		if(StringUtils.isBlank(channelOrder.getConsigneeMobile())) {
			throw new BaseServiceException("缺少必填参数 consigneeMobile");
		}
		
		o.setConsigneeMobile(channelOrder.getConsigneeMobile());
		if(StringUtils.isNotBlank( channelOrder.getChannelCreateTime() )) {
				try {
					
					o.setChannelCreateTime(DateTimeUtil.stampToDate(channelOrder.getChannelCreateTime()));
				} catch (Exception e) {
					//o.setChannelCreateTime(channelOrder.getChannelCreateTime());
					//throw new BaseServiceException("channelCreateTime 字段不是正确的时间格式");
					try {
						o.setChannelCreateTime(DateTimeUtil.getDateByString(channelOrder.getChannelCreateTime(), DateTimeUtil.LONG_TIME_FORMAT));
					}catch (Exception es) {
						// TODO: handle exception
						throw new BaseServiceException("channelCreateTime 字段不是正确的时间格式");
					}
				}
		}
		o.setOrderStatus(OrderStatus.accept.getName());
		if(StringUtils.isNotBlank(channelOrder.getShopMemberId()))o.setShopMemberId("");
		if(StringUtils.isNotBlank(channelOrder.getMemberName()))o.setMemberName(channelOrder.getMemberName());
		if(StringUtils.isNotBlank(channelOrder.getIsHdfk()))o.setIsHdfk(channelOrder.getIsHdfk());
		if(StringUtils.isNotBlank(channelOrder.getPromotionAmount()))o.setPromotionAmount(new BigDecimal(channelOrder.getPromotionAmount()));
		if(StringUtils.isBlank(channelOrder.getConsigneeName())) {
			throw new BaseServiceException("缺少必填参数 consigneeName");
		}
		o.setConsigneeName(channelOrder.getConsigneeName());
		if(StringUtils.isBlank(channelOrder.getFreightAmount())) {
			o.setFreightAmount(new BigDecimal("0"));
		}else {
			o.setFreightAmount(new BigDecimal(channelOrder.getFreightAmount()));
		}
		if(StringUtils.isBlank(channelOrder.getTotalAmount())) {
			o.setTotalAmount(new BigDecimal("0"));
		}else {
			o.setTotalAmount(new BigDecimal(channelOrder.getTotalAmount()));
		}
		if(StringUtils.isBlank(channelOrder.getPaidAmount())) {
			o.setPaidAmount(new BigDecimal("0"));
		}else {
			o.setPaidAmount(new BigDecimal(channelOrder.getPaidAmount()));
		}
		
		if(StringUtils.isBlank(channelOrder.getCashPaidAmount())) {
			o.setCashPaidAmount(new BigDecimal("0"));
		}else {
			o.setCashPaidAmount(new BigDecimal(channelOrder.getCashPaidAmount()));
		}
		
		if(StringUtils.isBlank(channelOrder.getBalancePaidAmount())) {
			o.setBalancePaidAmount(new BigDecimal("0"));
		}else {
			o.setBalancePaidAmount(new BigDecimal(channelOrder.getBalancePaidAmount()));
		}
		
		o.setPaidStatus(PaidStatus.PAY.getName());
		if(StringUtils.isBlank(channelOrder.getConsigneeProvinceName())) {
			throw new BaseServiceException("缺少必填参数 consigneeProvinceName");
		}
		o.setConsigneeProvinceName(channelOrder.getConsigneeProvinceName());
		
		if(StringUtils.isBlank(channelOrder.getConsigneeCityName())) {
			throw new BaseServiceException("缺少必填参数 consigneeCityName");
		}
		o.setConsigneeCityName(channelOrder.getConsigneeCityName());
		if(StringUtils.isBlank(channelOrder.getConsigneeRegionName())) {
			throw new BaseServiceException("缺少必填参数 consigneeRegionName");
		}
		o.setConsigneeRegionName(channelOrder.getConsigneeRegionName());
		if(StringUtils.isBlank(channelOrder.getConsigneeAddress())) {
			throw new BaseServiceException("缺少必填参数 consigneeAddress");
		}
		o.setConsigneeAddress(channelOrder.getConsigneeAddress());
		
		if(StringUtils.isBlank(channelOrder.getIsInvoice())) {
			throw new BaseServiceException("缺少必填参数 isInvoice");
		}
		o.setIsInvoice(channelOrder.getIsInvoice());
		o.setCreateTime(new Date());
		o.setUpdateTime(new Date());
		if(StringUtils.isNotBlank(channelOrder.getIsFreeze()))o.setIsFreeze(channelOrder.getIsFreeze());
		o.setIsMergeOrder(0); //不合单
		if(StringUtils.isNotBlank(channelOrder.getBuyerMemo()))o.setBuyerMemo(channelOrder.getBuyerMemo());
		if(StringUtils.isNotBlank(channelOrder.getBuyerMessage()))o.setBuyerMessage(channelOrder.getBuyerMessage());
		if(StringUtils.isBlank(channelOrder.getPayTime())) {
			throw new BaseServiceException("缺少必填参数 payTime");
		}
		o.setPayTime(channelOrder.getPayTime());
		o.setProcessStatus("PENDING");
		
		if(StringUtils.isNotBlank(channelOrder.getSupplierId())) {
			o.setSupplierId(channelOrder.getSupplierId());
		}
		if(StringUtils.isBlank(channelOrder.getSupplierName())) {
			throw new BaseServiceException("缺少必填参数 supplierName");
		}
		Supplier spr = new Supplier();
		spr.setFullName(channelOrder.getSupplierName());
		if(CollectionUtils.isEmpty(supplierService.queryAll(spr))) {
			throw new BaseServiceException( channelOrder.getSupplierName() + "这个供应商不是系统有效供应商,无法接收该供应商的订单");
		}
		o.setSupplierName(channelOrder.getSupplierName());
		
		
		if(StringUtils.isNotBlank(channelOrder.getIsOverseas())) {
			o.setIsOverseas(channelOrder.getIsOverseas());
		}
		if(StringUtils.isNotBlank(channelOrder.getConsigneeCardCode())) {
			o.setConsigneeCardCode(channelOrder.getConsigneeCardCode());
		}
		if(StringUtils.isNotBlank(channelOrder.getConsigneeCardPic())) {
			o.setConsigneeCardPic(channelOrder.getConsigneeCardPic());
		}
				
		return o;
	}

}
